﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.Design;
using System.ComponentModel.Design;
using System.Windows.Forms;
using FormsDesign = System.Windows.Forms.Design;

namespace AjaxSnippets.Design
{
    [System.Security.Permissions.SecurityPermission(
        System.Security.Permissions.SecurityAction.Demand,
        Flags = System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
    public class SnippetCallbackDesigner : ControlDesigner
    {
        public override string GetDesignTimeHtml()
        {
            return CreatePlaceHolderDesignTimeHtml();
        }

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection actionLists = new DesignerActionListCollection();
                actionLists.AddRange(base.ActionLists);
                
                actionLists.Add(new SnippetCallbackActionList(this));
                return actionLists;
            }
        }
    }

    public class SnippetCallbackActionList : DesignerActionList
    {
        SnippetCallbackDesigner _Designer;
        DesignerActionItemCollection _Items;
        public SnippetCallbackActionList(SnippetCallbackDesigner designer) : base(designer.Component)
        {
            _Designer = designer;
        }

        public void CopyStubs()
        {
            SnippetCallback snippetCallback = (SnippetCallback)Component;
            StringBuilder functionStubs = new StringBuilder();
            if (String.IsNullOrEmpty(snippetCallback.CallbackComponentId))
            {
                if (!String.IsNullOrEmpty(snippetCallback.BeforeCallbackMethodName))
                {
                    functionStubs.AppendFormat("function {0}()", snippetCallback.BeforeCallbackMethodName);
                    functionStubs.Append("{");
                    functionStubs.AppendLine();
                    functionStubs.AppendLine("}");
                }
                if(!String.IsNullOrEmpty(snippetCallback.CallbackSuccessMethodName))
                {
                    functionStubs.AppendFormat("function {0}(result)", snippetCallback.CallbackSuccessMethodName);
                    functionStubs.Append("{");
                    functionStubs.AppendLine();
                    functionStubs.AppendLine("}");
                }
                if (!String.IsNullOrEmpty(snippetCallback.CallbackFailureMethodName))
                {
                    functionStubs.AppendFormat("function {0}(error)", snippetCallback.CallbackFailureMethodName);
                    functionStubs.Append("{");
                    functionStubs.AppendLine();
                    functionStubs.AppendLine("}");
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(snippetCallback.BeforeCallbackMethodName))
                {
                    functionStubs.AppendFormat("{0}: function()", snippetCallback.BeforeCallbackMethodName);
                    functionStubs.Append("{");
                    functionStubs.AppendLine();
                    functionStubs.AppendLine("}");
                }
                if (!String.IsNullOrEmpty(snippetCallback.CallbackSuccessMethodName))
                {
                    functionStubs.AppendFormat("{0}: function(result)", snippetCallback.CallbackSuccessMethodName);
                    functionStubs.Append("{");
                    functionStubs.AppendLine();
                    functionStubs.AppendLine("}");
                }
                if (!String.IsNullOrEmpty(snippetCallback.CallbackFailureMethodName))
                {
                    functionStubs.AppendFormat("{0}: function(error)", snippetCallback.CallbackFailureMethodName);
                    functionStubs.Append("{");
                    functionStubs.AppendLine();
                    functionStubs.AppendLine("}");
                }
            }
            Clipboard.SetDataObject(functionStubs.ToString());
            this._Designer.UpdateDesignTimeHtml();
        }

        public void Configure()
        {
            ControlDesigner.InvokeTransactedChange(base.Component, new TransactedChangeCallback(this.ConfigureCallback), null, "Configure");
        }

        private bool ConfigureCallback(object context)
        {
            ConfigureSnippetCallbackWizard wizard = new ConfigureSnippetCallbackWizard(_Designer, Component.Site);
            if (base.Component.Site != null)
            {
                FormsDesign.IUIService service = (FormsDesign.IUIService)base.Component.Site.GetService(typeof(FormsDesign.IUIService));
                if (service != null)
                {
                    return service.ShowDialog(wizard) == DialogResult.OK;
                }
            }
            return wizard.ShowDialog() == DialogResult.OK;
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            if (_Items == null)
            {
                _Items = new DesignerActionItemCollection();
                _Items.Add(new DesignerActionMethodItem(this, "Configure", "Configure..."));

                _Items.Add(new DesignerActionMethodItem(this, "CopyStubs", "Javascript function stubs to clipboard"));
            }

            return _Items;
        }

    }
}
